package com.doge.calcalbum.widgets

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.annotation.ColorInt
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

abstract class GridItemDecoration(private val lineWidth: Int, @ColorInt colorRGB: Int) :
    ItemDecoration() {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = colorRGB
        style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        val childCount1 = parent.childCount
        for (i in 0 until childCount1) {
            val child = parent.getChildAt(i)
            val itemPosition = (child.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
            val sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition)
            if (sideOffsetBooleans[0]) {
                drawChildLeftVertical(child, c)
            }
            if (sideOffsetBooleans[1]) {
                drawChildTopHorizontal(child, c)
            }
            if (sideOffsetBooleans[2]) {
                drawChildRightVertical(child, c)
            }
            if (sideOffsetBooleans[3]) {
                drawChildBottomHorizontal(child, c)
            }
        }
    }

    private fun drawChildBottomHorizontal(child: View, c: Canvas) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin - lineWidth
        val right = child.right + params.rightMargin + lineWidth
        val top = child.bottom + params.bottomMargin
        val bottom = top + lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
    }

    private fun drawChildTopHorizontal(child: View, c: Canvas) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin - lineWidth
        val right = child.right + params.rightMargin + lineWidth
        val bottom = child.top - params.topMargin
        val top = bottom - lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
    }

    private fun drawChildLeftVertical(child: View, c: Canvas) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin - lineWidth
        val bottom = child.bottom + params.bottomMargin + lineWidth
        val right = child.left - params.leftMargin
        val left = right - lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
    }

    private fun drawChildRightVertical(child: View, c: Canvas) {
        val params = child
            .layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin - lineWidth
        val bottom = child.bottom + params.bottomMargin + lineWidth
        val left = child.right + params.rightMargin
        val right = left + lineWidth
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        val sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition)
        val left = if (sideOffsetBooleans[0]) lineWidth / 2 else 0
        val top = if (sideOffsetBooleans[1]) lineWidth else 0
        val right = if (sideOffsetBooleans[2]) lineWidth / 2 else 0
        val bottom = if (sideOffsetBooleans[3]) lineWidth else 0
        outRect[left, top, right] = bottom
    }

    abstract fun getItemSidesIsHaveOffsets(itemPosition: Int): Array<Boolean>

}